package com.lfg.leetcode.leetcode200;

public class No209minSubArrayLen {

    public static void main(String[] args) {
        int ret = minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3});
        System.out.println(ret);
    }


    public static int minSubArrayLen(int s, int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int ret = nums.length + 1;
        int total = 0;
        int start = 0;
        int end = 0;
        for (start = 0; start < nums.length; ) {
            if (total < s && end < nums.length) {
                total += nums[end++];
            } else {
                total -= nums[start++];
            }

            if (total >= s) {
                ret = Math.min(ret, end - start);
            }
        }
        if (ret > nums.length) {
            return 0;
        }
        return ret;
    }
}
